package com.paas.demo.controller;

import com.plat.paas.core.LockUtil;

import java.util.concurrent.TimeUnit;

@org.springframework.stereotype.Service
public class Service {

    public  Service(){
        System.out.println("new Service");
    }
    private int n = 500;

    public void seckill2() {
        String identifier = null;
        long start = System.currentTimeMillis();
        if (LockUtil.lock("testlock", 1, TimeUnit.SECONDS)){


            long lockTime = System.currentTimeMillis();
            System.out.println("lockTime cost millis = " + (lockTime - start));

            System.out.println(Thread.currentThread().getName() + "获得了锁, n = " + n--);


            LockUtil.unlock();
            long unLockTime = System.currentTimeMillis();
            System.out.println("unLockTime cost millis = " + (unLockTime - lockTime));

        }else {
            System.out.println(Thread.currentThread().getName() + "获得了锁失败 ");
        }
        long end = System.currentTimeMillis();

        System.out.println("seckill cost millis = " + (end - start));
    }
}
